using System.Collections.Generic;
using System.Linq;

using CommonBehaviors.Actions;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System;
using System.Reflection;

using Action = Styx.TreeSharp.Action;


namespace Honorbuddy.Quest_Behaviors.PressButton
{
    [CustomBehaviorFileName(@"PressButton")]
    public class PressButton : CustomForcedBehavior
    {
        public PressButton(Dictionary<string, string> args)
            : base(args)
        {
            try
            {
                if (!IsDone)
                {
                    string button;

                    button = GetAttributeAs<string>("Button", false, null, null);
                    Start(button);
                }
            }
            catch (Exception except)
            {
                LogMessage("error", "BEHAVIOR MAINTENANCE PROBLEM: " + except.Message
                        + "\nFROM HERE:\n"
                        + except.StackTrace + "\n");
                IsAttributeProblem = true;
            }

        }

        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        private void Start(string button)
        {
			if (button == "escape" || button == "Escape")
            {
                Styx.Helpers.KeyboardManager.KeyUpDown((char)ConsoleKey.Escape);
                System.Threading.Thread.Sleep(100);
            }
            _isBehaviorDone = true;
        }

        private bool _isBehaviorDone;
        private Composite _root;

        public override bool IsDone
        {
            get
            {
                return _isBehaviorDone;
            }
        }

        public override void OnStart()
        {
            OnStart_HandleAttributeProblem();

        }


        protected override Composite CreateBehavior()
        {
            return _root ?? (_root = new PrioritySelector(DoneYet));
        }



        public bool IsComplete()
        {
            bool complete = _isBehaviorDone;
            return complete;
        }

        public Composite DoneYet
        {
            get
            {
                return
                    new Decorator(ret => IsComplete(), new Action(delegate
                    {
                        TreeRoot.StatusText = "Finished!";
                        _isBehaviorDone = true;
                        return RunStatus.Success;
                    }));

            }
        }
    }
}
